package com.app.demo.imageEdit;


import android.annotation.SuppressLint;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.app.demo.MainActivity;
import com.app.demo.R;
import com.app.demo.db.MyDBHelper;
import com.nostra13.universalimageloader.core.ImageLoader;


public class loginActivity extends AppCompatActivity {
//1定义对象
    EditText et_name,et_pwd;
    Button btn_bewregister,btn_login;
    MyDBHelper mhelper;
    SQLiteDatabase db;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        //2绑定控件
        initView();

        //3登录按钮功能的实现
        btnLogin();
        //4新用户注册按钮功能的实现
        btnNewRegister();

        ImageView imageView = findViewById(R.id.bg);
        String imageUri = "drawable://" + R.drawable.ic_launcher_background;
        ImageLoader.getInstance().displayImage(imageUri, imageView);
    }


    //2绑定控件----------代码
    private void initView() {
        et_name=findViewById(R.id.et_name_lg);
        et_pwd=findViewById(R.id.et_pwd_lg);
        btn_bewregister=findViewById(R.id.bt_newregister_lg);
        btn_login=findViewById(R.id.bt_login_lg);
        mhelper=new MyDBHelper(loginActivity.this);
        db=mhelper.getWritableDatabase();
    }
    //3登录按钮功能的实现----------代码
    private void btnLogin() {
        btn_login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //首先：获取输入的用户名和密码
                String inputname = et_name.getText().toString();
                String inputpwd = et_pwd.getText().toString();
                //其次：对获取的用户名和密码进行判断
                if (inputname.equals("") || inputpwd.equals("")) {
                    Toast.makeText(loginActivity.this, "用户名或密码不能为空", Toast.LENGTH_SHORT).show();
                } else {//用户名或密码不为空时，我们再对输入的正确性进行判断
                    //根据输入的用户名和密码从数据库中查询
                    Cursor cursor = db.rawQuery("select * from tb_userinfo  where name=? and pwd=?", new String[]{inputname, inputpwd});
                    //根据查询到的结果进行判断
                    if (cursor.moveToNext()) {//查询到时
                        @SuppressLint("Range") String getname = cursor.getString(cursor.getColumnIndex("name"));
                        @SuppressLint("Range") String getpwd = cursor.getString(cursor.getColumnIndex("pwd"));
                        if (inputname.equalsIgnoreCase(getname) && inputpwd.equalsIgnoreCase(getpwd)) {
                            Toast.makeText(loginActivity.this, "用户名和密码正确，欢迎登录", Toast.LENGTH_SHORT).show();
                            Intent intent = new Intent(loginActivity.this, MainActivity.class);
                            startActivity(intent);
                            finish();
                        }
                    } else {
                        Toast.makeText(loginActivity.this, "用户名或密码错误，请重新输入", Toast.LENGTH_SHORT).show();
                        et_name.setText("");
                        et_pwd.setText("");
                    }
                }
            }
        });
    }
    //4新用户注册按钮功能的实现--------代码
    private void btnNewRegister() {
        btn_bewregister.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(loginActivity.this, registerActivity.class);
                startActivity(intent);
            }
        });
    }

}